﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL_V1.App_Code.DBBroker;
using EntityObjects;

namespace BAL.Controller
{
    public class ViewDisbursementListForStoreControl
    {
        DepartmentDBBroker departmentRef;
        DisbursementDBBroker disbursementRef;
        RequisitionDBBroker requisitionRef;
        CollectionPointDBBroker collectionRef;
        EmployeeDBBroker employeeRef;
        public ViewDisbursementListForStoreControl()
        {
            departmentRef = DepartmentDBBroker.getDepartmentDBBroker();
            disbursementRef = DisbursementDBBroker.getDisbursementDBBroker();
            requisitionRef = RequisitionDBBroker.getRequisitionDBBroker();
            collectionRef = CollectionPointDBBroker.getCollectionPointDBBroker();
            employeeRef = EmployeeDBBroker.getEmployeeDBBroker();
        }
        public Department getDeptInfo(string deptid)
        {
            return departmentRef.selectDepartment(deptid);
        }
        public CollectionPoint getColPt(int id)
        {
            return collectionRef.selectCollectionPoint(id);
        }
        public Employee getEmp(string id)
        {
            return employeeRef.selectEmployee(id);
        }
        //get all dept
        public List<Department> getAllDepartment()
        {
            return departmentRef.selectAllDepartment();
        }

        //get all disbursed department
        public List<Department> getDisbursedDepartment()
        {
            return disbursementRef.getDepartmentByDisbursementAssignedStatus();
        }
       
        //update disbursement status
        public Boolean updateDisbursementStatus(String status, int disId)
        {
            //Disbursement d = disbursementRef.getDisbursementByDeptIdAndAssignedStatus(deptId);
            Disbursement d = disbursementRef.selectDisbursement(disId);
            d.Status = status;
            int row = disbursementRef.updateDisbursement(d, d.DisbursementId);
            if (row == 1)
                return true;
            else
                return false;
        }

        
        
        
        public List<DisbursementCatalogue> getDisbursementByDeptId(String deptId)
        {
            return disbursementRef.getDisbursementAndCatalogue(deptId);
        }

        //get disbursement status with assigned status
        public List<DisbursementCatalogue> getDisbursementItemToUpdate()
        {
            bool flag=true;
            List<DisbursementCatalogue> resultList = new List<DisbursementCatalogue>();
            List<DisbursementCatalogue> dis= disbursementRef.getDisbursementAndCatalogueAndCollection();
            foreach (DisbursementCatalogue item in dis)
            {
                List<ConsolidateRetrivalItem> reqCountList = disbursementRef.getRequisitionItemCount(item.DeptId);
                if (reqCountList != null)
                {
                    foreach (ConsolidateRetrivalItem crItem in reqCountList)
                    {
                        ConsolidateRetrivalItem CRI = disbursementRef.getDisbursementItemCount(item.DeptId, crItem.ItemId);
                        if (CRI != null)
                        {
                            //if (CRI.ItemCount != crItem.ItemCount)
                            //    flag = false;
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }
                else
                {
                    resultList.Add(item);
                }
                if (flag)
                    resultList.Add(item);

            }



            return resultList;
        }
        public List<DisbursementCatalogue> getDisbursementItemToUpdate(string deptid)
        {
            List<DisbursementCatalogue> rst = new List<DisbursementCatalogue>();
            rst.Add(disbursementRef.getDisbursementAndCatalogueAndCollectionByDeptId(deptid));
            return rst;
        }

        
    }
}
